package ophiux.wechatpay.sdk.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;


public class XMLFormat {
	
	 /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        byte[] data = outPut("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx394240ecef75c40a]]></appid><mch_id><![CDATA[1453527302]]></mch_id><nonce_str><![CDATA[Z1Kvz8i4JwLqc92g]]></nonce_str><sign><![CDATA[072A015D574193897D2E8CC865612DE9]]></sign><result_code><![CDATA[SUCCESS]]></result_code><openid><![CDATA[oa3s41iq47I3lI5_3_aYntcPz_QU]]></openid><is_subscribe><![CDATA[Y]]></is_subscribe><trade_type><![CDATA[MICROPAY]]></trade_type><bank_type><![CDATA[CFT]]></bank_type><total_fee>100</total_fee><fee_type><![CDATA[CNY]]></fee_type><transaction_id><![CDATA[4004512001201708013871433641]]></transaction_id><out_trade_no><![CDATA[4005120012017072525232135962]]></out_trade_no><attach><![CDATA[]]></attach><time_end><![CDATA[20170801171114]]></time_end><trade_state><![CDATA[SUCCESS]]></trade_state><cash_fee>100</cash_fee></xml>");  
          
        if (data != null) {  
            try {  
                System.out.println(new String(data, "UTF-8"));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
          
    }  
	 /** 
     * 格式化XML输出. 
     * @param xmlStr xml文本内容. 
     * @return 
     */  
    public static byte[] outPut(String xmlStr) {  
          
        byte[] data = null;  
        StringReader stringReader = null;  
          
        try {  
              
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
            DocumentBuilder db = dbf.newDocumentBuilder();  
            stringReader =  new StringReader(xmlStr);  
            InputSource is = new InputSource(stringReader);  
            Document doc = db.parse(is);  
            data = outPut(doc);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (stringReader != null) {  
                stringReader.close();  
            }  
        }  
        return data;  
    }  
    
    /** 
     * 格式化输出. 
     * @param node 
     * @return 
     */  
    public static byte[] outPut(Node node) {  
        ByteArrayOutputStream byteArrayOutputStream = null;  
        byte[] data = null;  
          
        try {  
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();      
            DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0");  
            LSSerializer serializer = impl.createLSSerializer();  
              
            DOMConfiguration domConfiguration = serializer.getDomConfig();  
            boolean isSupport = domConfiguration.canSetParameter("format-pretty-print", true);  
            if (isSupport) {  
                domConfiguration.setParameter("format-pretty-print", true);  
            }  
              
            LSOutput output = impl.createLSOutput();  
            output.setEncoding("UTF-8");  
            byteArrayOutputStream = new ByteArrayOutputStream();  
            output.setByteStream(byteArrayOutputStream);  
            serializer.write(node, output);  
            data = byteArrayOutputStream.toByteArray();  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (byteArrayOutputStream != null) {  
                try {  
                    byteArrayOutputStream.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
              
        }  
        return data;  
    } 
}
